package com.qdxwx.data.service.website;


import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.website.WebsiteNavigateMapper;
import com.qdxwx.models.website.WebsiteNavigate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * WebsiteNavigate接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class WebsiteNavigateServiceImpl implements WebsiteNavigateService {
	@Autowired
	private WebsiteNavigateMapper websiteNavigateMapper;

	@Override
	public List<WebsiteNavigate> getList(WebsiteNavigate websiteNavigate, PageRequest page) {
		return websiteNavigateMapper.selectList(websiteNavigate, page);
	}

	@Override
	public long count(WebsiteNavigate websiteNavigate) {
		return websiteNavigateMapper.selectCount(websiteNavigate);
	}

	@Override
	public int insert(WebsiteNavigate websiteNavigate) {
		int row = websiteNavigateMapper.insertWebsiteNavigate(websiteNavigate);
		if (row > 0) CacheUtils.remove(CacheConstants.WEBSITE_NAVIGATE + ":" + websiteNavigate.getType());
		return row;
	}

	@Override
	public int updateStatus(WebsiteNavigate websiteNavigate) {
		int row = websiteNavigateMapper.updateStatus(websiteNavigate);
		if (row > 0) CacheUtils.remove(CacheConstants.WEBSITE_NAVIGATE + ":" + websiteNavigate.getType());
		return row;
	}

	@Override
	public int delete(int id) {
		int row = websiteNavigateMapper.deleteById(id);
		if (row > 0) CacheUtils.remove(CacheConstants.WEBSITE_NAVIGATE);
		return row;
	}

	@Override
	public int update(WebsiteNavigate websiteNavigate) {
		int row = websiteNavigateMapper.updateWebsiteNavigate(websiteNavigate);
		if (row > 0) CacheUtils.remove(CacheConstants.WEBSITE_NAVIGATE + ":" + websiteNavigate.getType());
		return row;
	}

	@Override
	public WebsiteNavigate get(int id) {
		return websiteNavigateMapper.selectWebsiteNavigateById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WebsiteNavigate> getListByType(String type) {
		String cacheKey = CacheConstants.WEBSITE_NAVIGATE + ":" + type;
		List<WebsiteNavigate> navList = (List<WebsiteNavigate>) CacheUtils.get(cacheKey);
		if (navList == null) {
			WebsiteNavigate query = new WebsiteNavigate();
			query.setType(type);
			query.setStatus(0);
			navList = getList(query, null);
			if (navList != null && navList.size() > 0) {
				CacheUtils.set(cacheKey, navList, CacheConstants.WEBSITE_NAVIGATE_TIME);
			}
		}
		return navList;
	}
}